package com.infoholdcity.common.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.infoholdcity.common.common.LoginUser;
import com.infoholdcity.common.common.SysUserCacheInfo;
import com.infoholdcity.common.common.model.DictModel;
import com.infoholdcity.common.common.model.SysPermissionDataRuleModel;
import com.infoholdcity.common.constant.CacheConstant;
import com.infoholdcity.common.entity.SysPermission;
import com.infoholdcity.common.entity.SysUser;
import com.infoholdcity.common.mapper.SysPermissionMapper;
import com.infoholdcity.common.mapper.SysUserMapper;
import com.infoholdcity.common.mapper.SysUserRoleMapper;
import com.infoholdcity.common.service.CommonAPI;
import com.infoholdcity.common.util.oConvertUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@Service
@DS("slave_1")
public class SysBaseApiImpl implements CommonAPI {

    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    @Resource
    private SysPermissionMapper sysPermissionMapper;

    @Resource
    private SysUserMapper userMapper;

    /**
     *查询用户拥有的角色集合
     * @param username
     * @return Set<String>
     */
    @Override
    public Set<String> queryUserRoles(String username) {
        // 查询用户拥有的角色集合
        List<String> roles = sysUserRoleMapper.getRoleByUserName(username);
        log.info("-------通过数据库读取用户拥有的角色Rules------username： " + username + ",Roles size: " + (roles == null ? 0 : roles.size()));
        return new HashSet<>(roles);
    }

    @Override
    public Set<String> queryUserAuths(String username) {
        Set<String> permissionSet = new HashSet<>();
        List<SysPermission> permissionList = sysPermissionMapper.queryByUser(username);
        for (SysPermission po : permissionList) {
//			// TODO URL规则有问题？
//			if (oConvertUtils.isNotEmpty(po.getUrl())) {
//				permissionSet.add(po.getUrl());
//			}
            if (oConvertUtils.isNotEmpty(po.getPerms())) {
                permissionSet.add(po.getPerms());
            }
        }
        log.info("-------通过数据库读取用户拥有的权限Perms------username： "+ username+",Perms size: "+ (permissionSet==null?0:permissionSet.size()) );
        return permissionSet;
    }

    @Override
    @Cacheable(cacheNames= CacheConstant.SYS_USERS_CACHE, key="#username")
    public LoginUser getUserByName(String username) {
        if(oConvertUtils.isEmpty(username)) {
            return null;
        }
        LoginUser loginUser = new LoginUser();
        SysUser sysUser = userMapper.getUserByName(username);
        if(sysUser==null) {
            return null;
        }
        BeanUtils.copyProperties(sysUser, loginUser);
        return loginUser;
    }

    @Override
    public String translateDictFromTable(String table, String text, String code, String key) {
        return "";
    }

    @Override
    public String translateDict(String code, String key) {
        return "";
    }

    @Override
    public List<SysPermissionDataRuleModel> queryPermissionDataRule(String component, String requestPath, String username) {
        return Collections.emptyList();
    }

    @Override
    public SysUserCacheInfo getCacheUser(String username) {
        return null;
    }

    @Override
    public List<DictModel> queryDictItemsByCode(String code) {
        return Collections.emptyList();
    }

    @Override
    public List<DictModel> queryEnableDictItemsByCode(String code) {
        return Collections.emptyList();
    }

    @Override
    public List<DictModel> queryTableDictItemsByCode(String table, String text, String code) {
        return Collections.emptyList();
    }

    @Override
    public Map<String, List<DictModel>> translateManyDict(String dictCodes, String keys) {
        return Collections.emptyMap();
    }

    @Override
    public List<DictModel> translateDictFromTableByKeys(String table, String text, String code, String keys) {
        return Collections.emptyList();
    }
}
